package com.api.gbhg.zengping.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.zengping.dao.JobAdministrationDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2020-12-30 16:02
 *
 * 海关在职行政人员工资津补贴经费版本 核批
 **/
public class JobAdministrationAction {

    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    // 操作sql类
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    private JobAdministrationDao getJoba=new JobAdministrationDao();
    // 操作sql类
    private JobAdministrationDao joba=new JobAdministrationDao();
    @POST
    @Path("/jobAdministrationBatch")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String jobAdministrationBatch(Map<String,String> param) {
        // 得到核批状态 3就是已核批  非3就是待核批
        String requestId = param.get("requestId");
        LOGGER.info("海关在职核批参数："+requestId);
        RecordSetTrans rst = joba.getSBInfo(requestId);
        Map<String,Object>[] maps=new HashMap[rst.getArray().size()];
        int i=0;
        while (rst.next()){
            maps[i]=new HashMap<>();
            //序号id
            maps[i].put("key",i);
            // 流程记录id
            maps[i].put("lcjlsqjlid",rst.getString("lcjlsqjlid"));
            maps[i].put("xmmin",rst.getString("xmmin"));
            // 申报单位
            RecordSet sbdw = rmcd.getSBDWById(rst.getInt("sbdw"));
            sbdw.next();
            maps[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME"));
            // 申报科室
            RecordSet sbks = rmcd.getSBKSById(rst.getInt("sbks"));
            sbks.next();
            maps[i].put("sbks",sbks.getString("departmentmark"));
            //编报人
            RecordSet jbr = rmcd.getUserNameById(rst.getInt("fzr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }
            maps[i].put("fzr",loginid);
            maps[i].put("yszje",rst.getString("yszje"));
            maps[i].put("ygzffrs",rst.getString("ygzffrs"));
            maps[i].put("zwgz",rst.getString("zwgz"));
            maps[i].put("jbgz",rst.getString("jbgz"));
            maps[i].put("gzxjt",rst.getString("gzxjt"));
            maps[i].put("shxbt",rst.getString("shxbt"));
            maps[i].put("gxjt",rst.getString("gxjt"));

            maps[i].put("nzycxjj",rst.getString("nzycxjj"));
            maps[i].put("tqjt",rst.getString("tqjt"));
            maps[i].put("yxwxnxjbc",rst.getString("yxwxnxjbc"));
            maps[i].put("jswmj",rst.getString("jswmj"));
            maps[i].put("jhsyj",rst.getString("jhsyj"));
            maps[i].put("lsbt",rst.getString("lsbt"));
            maps[i].put("dsznfmjlf",rst.getString("dsznfmjlf"));
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo();
        String tableName="uf_hgzzxzrygzjbtjfbb";
        RecordSetTrans sbInfoCount = getJoba.getSBInfoCount(tableName,requestId);
        sbInfoCount.next();
        int pageCount = sbInfoCount.getInt("num");
        pageInfo.setTotal(pageCount);
        pageInfo.setList(maps);
        pageInfo.setFromTable(tableName);
        return JSON.toJSONString(pageInfo);
    }

    /**
     * 海关在职
     * @param requestId
     * @return
     */
    public String jobAdministrationBatch1(String requestId) {
        // 得到核批状态 3就是已核批  非3就是待核批
        LOGGER.info("海关在职核批参数："+requestId);
        RecordSetTrans rst = joba.getSBInfo(requestId);
        Map<String,Object>[] maps=new HashMap[rst.getArray().size()];
        int i=0;
        while (rst.next()){
            maps[i]=new HashMap<>();
            //序号id
            maps[i].put("key",i);
            // 流程记录id
            maps[i].put("lcjlsqjlid",rst.getString("lcjlsqjlid"));
            maps[i].put("xmmin",rst.getString("xmmin"));
            // 申报单位
            RecordSet sbdw = rmcd.getSBDWById(rst.getInt("sbdw"));
            sbdw.next();
            maps[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME"));
            // 申报科室
            RecordSet sbks = rmcd.getSBKSById(rst.getInt("sbks"));
            sbks.next();
            maps[i].put("sbks",sbks.getString("departmentmark"));
            //编报人
            RecordSet jbr = rmcd.getUserNameById(rst.getInt("fzr"));
            jbr.next();
            String lastname = jbr.getString("lastname");
            if(lastname==""){
                lastname="系统管理员";
            }

            maps[i].put("fzr",lastname);
            BigDecimal yszje = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("yszje")));
            maps[i].put("yszje",yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            maps[i].put("ygzffrs",rst.getString("ygzffrs"));

            BigDecimal zwgz = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("zwgz")));
            maps[i].put("zwgz",zwgz.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal jbgz = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("jbgz")));
            maps[i].put("jbgz",jbgz.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal gzxjt = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("gzxjt")));
            maps[i].put("gzxjt",gzxjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal shxbt = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("shxbt")));
            maps[i].put("shxbt",shxbt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal gxjt = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("gxjt")));
            maps[i].put("gxjt",gxjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal nzycxjj = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("nzycxjj")));
            maps[i].put("nzycxjj",nzycxjj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal tqjt = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("tqjt")));
            maps[i].put("tqjt",tqjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal tqyxwxnxjbcjt = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("yxwxnxjbc")));
            maps[i].put("yxwxnxjbc",tqyxwxnxjbcjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal jswmj = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("jswmj")));
            maps[i].put("jswmj",jswmj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal jhsyj = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("jhsyj")));
            maps[i].put("jhsyj",jhsyj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal lsbt = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("lsbt")));
            maps[i].put("lsbt",lsbt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

            BigDecimal dsznfmjlf = new BigDecimal(FormatUtil.DmFormat(rst.getDouble("dsznfmjlf")));
            maps[i].put("dsznfmjlf",dsznfmjlf.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo();
        String tableName="uf_hgzzxzrygzjbtjfbb";
        RecordSetTrans sbInfoCount = getJoba.getSBInfoCount(tableName,requestId);
        sbInfoCount.next();
        int pageCount = sbInfoCount.getInt("num");
        pageInfo.setTotal(pageCount);
        pageInfo.setList(maps);
        pageInfo.setFromTable(tableName);
        return JSON.toJSONString(pageInfo);
    }

    public Map getParam(Map<String,Integer> param){
        Integer pageNum= Integer.valueOf(param.get("pageNum"));
        param.put("pageNum",pageNum<1?1:pageNum);
        return param;
    }
}
